﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Besture.Common.Definition;
using Besture.Common.Helper;

namespace Besture.Common.Solution
{
    public class SolutionManager
    {
        /// <summary>
        /// 修改CsProj文件
        /// </summary>
        /// <returns></returns>
        public static void UpdateCsProj()
        {
            List<string> listAspxFile = IoHelper.GetFile("/Admin/CommonModel/AutoMatic/",".aspx");
            List<string> listAllFile = IoHelper.GetFile("/Admin/CommonModel/AutoMatic/");

            string ItemGroupInclude = string.Empty;
            string ItemGroupCompileInculdeStr = string.Empty;

            for (int i = 0; i < listAspxFile.Count; i++)
            {
                ItemGroupInclude += string.Format(@"<Content Include=""Admin\CommonModel\AutoMatic\{0}"" />", listAspxFile[i]);
            }

            for (int i = 0; i < listAspxFile.Count; i++)
            {
                ItemGroupCompileInculdeStr +=
                    string.Format(
                        @"<Compile Include=""Admin\CommonModel\AutoMatic\{0}.cs"">
                                <DependentUpon>{0}</DependentUpon>
                                <SubType>ASPXCodeBehind</SubType>
                            </Compile>
                            <Compile Include=""Admin\CommonModel\AutoMatic\{0}.designer.cs"">
                                <DependentUpon>{0}</DependentUpon>
                            </Compile>
                        ", listAspxFile[i]);
            }

            string csProjPath = @"/Besture.UI.csproj";

            //获取CsProj文件内容
            string csProjTxt = IoHelper.ReadFile(csProjPath);

            string tag = "<ItemGroup />";

            csProjTxt = csProjTxt.Remove(csProjTxt.IndexOf(tag));

            string autoMaticTxt = string.Format(@"    {0}
                                                        <ItemGroup>{1}</ItemGroup>
                                                        <ItemGroup>{2}</ItemGroup>
                                                    </Project>",tag, ItemGroupInclude
                                                ,ItemGroupCompileInculdeStr);

            csProjTxt += autoMaticTxt;


            System.IO.StreamWriter streamWriter = System.IO.File.CreateText(System.Web.HttpContext.Current.Server.MapPath(csProjPath));
            streamWriter.Write(csProjTxt);
            streamWriter.Dispose();
            streamWriter.Close();

            MsBuild(csProjPath);
        }


        /// <summary>
        /// 编译Solution
        /// </summary>
        /// <returns></returns>
        public static bool MsBuild(string csProjPath)
        {
            bool flag = false;
            string exePath;

            ProcessStartInfo startinfo;
            try
            {
                string cmd = string.Format("{0} /property:Configuration=Debug",System.Web.HttpContext.Current.Server.MapPath(csProjPath));
                startinfo = new ProcessStartInfo();
                startinfo.FileName = AppSettings.GetMsBuildPath();
                startinfo.Arguments = cmd;
                startinfo.WindowStyle = ProcessWindowStyle.Hidden;
                Process process = new Process();
                process.StartInfo = startinfo;
                process.Start();
                process.WaitForExit();
                if (process.HasExited)
                {
                    flag = true;
                }
                process.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
            return flag;
        }

    }
}
